package main

import (
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	var content string
	if len(os.Args) != 2 {
		fmt.Fprintf(os.Stderr, "参数错误！")
		os.Exit(1)
	} else {
		filePath := os.Args[1]
		data, err := ioutil.ReadFile(filePath)
		if err != nil {
			fmt.Fprintf(os.Stderr, "File reading error: %v", err)
			os.Exit(1)
		}
		content = string(data)
	}
	result, sTable, err := Analyse(content)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v", err)
		os.Exit(1)
	}
	fmt.Println("----- C语言词法分析 -----")
	fmt.Printf("源程序：\n")
	fmt.Printf("%s", content)
	fmt.Printf("\n\nResult：\n")
	for i := 0; i < len(result); i++ {
		fmt.Printf("%s\n", result[i])
	}
	fmt.Printf("\n符号表：\n")
	sTable.print()
	fmt.Println("----- End -----")
}
